package com.newsmanagement.service.impl;

import com.newsmanagement.common.Constants;
import com.newsmanagement.dao.IResourceRoleServiceDao;
import com.newsmanagement.dao.IResourceServiceDao;
import com.newsmanagement.dao.ISocialDevelopmentServiceDao;
import com.newsmanagement.entity.ReturnedMessages;
import com.newsmanagement.service.IResourceService;
import com.newsmanagement.service.ISocialDevelopmentService;
import com.newsmanagement.util.CheckAndChangeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xbr
 * 2017/12/4 16:29
 * 资源管理service实现
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ResourceServiceImpl implements IResourceService {


    @Autowired
    private IResourceServiceDao resourceServiceDao;

    @Autowired
    private IResourceRoleServiceDao resourceRoleServiceDao;


    /**
     * 添加资源
     * @param userId 操作人id
     * @param resourceName 资源名称
     * @param resourceInfo 资源内容
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages addResource(Integer userId ,String resourceName ,String resourceInfo){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"操作人主键未找到");
        }

        //验证资源名称
        if(!CheckAndChangeUtil.checkStringHaveData(resourceName)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑资源名称未找到");
        }

        //验证资源内容
        if(!CheckAndChangeUtil.checkStringHaveData(resourceInfo)){
            resourceInfo = "";
        }

        //添加资源内容信息
        resourceServiceDao.saveResource(userId ,resourceName ,resourceInfo);

        return returnedMessages;
    }

    /**
     * 修改资源
     * @param userId 操作人id
     * @param resourceId 资源id
     * @param resourceName 资源名称
     * @param resourceInfo 资源内容
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages updateResource(Integer userId , Integer resourceId ,String resourceName ,String resourceInfo){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"操作人主键未找到");
        }

        //验证资源id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(resourceId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"资源主键未找到");
        }

        //验证资源名称
        if(!CheckAndChangeUtil.checkStringHaveData(resourceName)){
            return returnedMessages.ReturnedMessagesFail(500 ,"请编辑资源名称未找到");
        }

        //验证资源内容
        if(!CheckAndChangeUtil.checkStringHaveData(resourceInfo)){
            resourceInfo = "";
        }

        //修改资源内容信息
        resourceServiceDao.updateResource(userId , resourceId ,resourceName ,resourceInfo);

        return returnedMessages;
    }

    /**
     * 删除资源
     * @param userId 操作人id
     * @param resourceId 资源id
     * @return ReturnedMessages
     */
    @Override
    public ReturnedMessages deleteResource(Integer userId , Integer resourceId){
        //格式化返回处理结果
        ReturnedMessages returnedMessages = new ReturnedMessages();

        //验证用户id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(userId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"操作人主键未找到");
        }

        //验证资源id
        if(!CheckAndChangeUtil.checkIntMoreThanZero(resourceId)){
            return returnedMessages.ReturnedMessagesFail(500 ,"资源主键未找到");
        }

        //逻辑删除资源信息
        resourceServiceDao.deleteResource(userId , resourceId);
        //根据资源id物理删除角色与资源信息表数据
        resourceRoleServiceDao.deleteResourceRoleByResourceId(userId , resourceId);

        return returnedMessages;
    }

}
